//
//  JXBaseView.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/27.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit


//注册.登录按钮点击事件协议
protocol JXBaseViewDelegate : NSObjectProtocol {
    func baseViewRegister()
    func baseViewLogin()
}

class JXBaseView: UIView {
    
    
    //MARK: - 代理属性
    weak var baseViewDelegate : JXBaseViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    // 从xib/storyboard里面加载会走这个方法
    // 当子类实现init(frame: CGRect)就不会继承父类的构造函数init?(coder aDecoder: NSCoder),就不能从xib/storyboard里面加载
    // 只有同时实现init(frame: CGRect)和init?(coder aDecoder: NSCoder)才能保证view即可以使用代码创建,也可以使用xib/storyboar来创建
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        // 做自己的事情
        super.init(coder: aDecoder)
    }
    
    //MARK: - 旋转动画
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration  = 20
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    
    
    func prepareUI() {
        // 1.将懒加载的控件添加到父控件里面
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮 CenterX
        // 约束都添加到父控件上面
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 转轮 CenterY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 房子
        // CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // CenterY和转轮CenterY重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // messageLabel
        // 宽度 240
        // 注意: 当toItem为nil时,attribute必须填:NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Top和转轮Bottom间距16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 注册按钮
        // 左边和消息的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        // Top和消息Bottom间距16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右边和消息的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        // Top和注册按钮的Top对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
        //顶部
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //左边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //右边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //底部
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    func setupVistotInfo (imageName : String , message : String) {
    
        iconView.image = UIImage(named:imageName)
        
        messageLabel.text = message
        
        messageLabel.sizeToFit()
        
        homeView.hidden = true
        
        sendSubviewToBack(coverView)
    }
    
    // MARK: - 懒加载
    // 小房子
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
   
    //覆盖层
    lazy var coverView: UIImageView = UIImageView(image: UIImage (named: "visitordiscover_feed_mask_smallicon"))
    
    
    // 转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 消息Label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "关注一些人,看看有上面惊喜"
//        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.grayColor()
        
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "registerButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    //注册按钮注册按钮点击方法
    
    func registerButtonDidClick() {
        baseViewDelegate?.baseViewRegister()
    }
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
         button.addTarget(self, action: "loginButtonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    //登陆按钮注册按钮点击方法
    func loginButtonDidClick() {
        baseViewDelegate?.baseViewLogin()
    }
}
